import java.util.ArrayList;
import java.util.List;

public class Solution2 {
    public static List<List<Integer>> ret = new ArrayList<>();
    public static List<Integer> path = new ArrayList<>();

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        subsets(nums);
        for (List<Integer> list : ret) {
            System.out.println(list);
        }
    }
    public static List<List<Integer>> subsets(int[] nums) {
        dfs2(nums,0);
        return ret;
    }

    //解法一：
    public static void dfs(int[] nums, int i) {
        if(nums.length  == i) {
            ret.add(new ArrayList<>(path));
            return;
        }
        //不选
        dfs(nums,i + 1);
        //选
        path.add(nums[i]);
        dfs(nums, i + 1);
        path.remove(path.size() - 1);
    }

    public static void dfs2(int[] nums, int pos) {
        ret.add(new ArrayList<>(path));

        for(int i = pos; i < nums.length; i++) {
            path.add(nums[i]);
            dfs2(nums, i + 1);
            path.remove(path.size() - 1);// 回溯
        }
    }
}
